//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.amazonaws.services.s3.transfer

import com.amazonaws.annotation.NotThreadSafe
import com.amazonaws.annotation.SdkTestInternalApi
import com.amazonaws.client.builder.ExecutorFactory
import com.amazonaws.internal.SdkFunction
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.transfer.TransferManager
import com.amazonaws.services.s3.transfer.TransferManagerConfiguration
import com.amazonaws.services.s3.transfer.TransferManagerParams
import com.amazonaws.services.s3.transfer.internal.TransferManagerUtils
import java.util.concurrent.ExecutorService

@NotThreadSafe
class MyTransferManagerBuilder @SdkTestInternalApi internal constructor(private val transferManagerFactory: SdkFunction<TransferManagerParams?, TransferManager?>) {

    var s3Client: AmazonS3? = null
    var executorFactory: ExecutorFactory? = null
    var isShutDownThreadPools: Boolean? = null
    var minimumUploadPartSize: Long? = null
    var multipartUploadThreshold: Long? = null
    var multipartCopyThreshold: Long? = null
    var multipartCopyPartSize: Long? = null
    var isDisableParallelDownloads: Boolean? = null

    private constructor() : this(DEFAULT_TRANSFER_MANAGER_FACTORY)

    fun withS3Client(s3Client: AmazonS3?): MyTransferManagerBuilder {
        this.s3Client = s3Client
        return this
    }

    private fun resolveS3Client(): AmazonS3 {
        return if (this.s3Client == null) AmazonS3ClientBuilder.defaultClient() else s3Client!!
    }

    fun withExecutorFactory(executorFactory: ExecutorFactory?): MyTransferManagerBuilder {
        this.executorFactory = executorFactory
        return this
    }

    private fun resolveExecutorService(): ExecutorService {
        return (if (this.executorFactory == null) TransferManagerUtils.createDefaultExecutorService() else executorFactory!!.newExecutor()) as ExecutorService
    }

    fun withShutDownThreadPools(shutDownThreadPools: Boolean?): MyTransferManagerBuilder {
        this.isShutDownThreadPools = shutDownThreadPools
        return this
    }

    private fun resolveShutDownThreadPools(): Boolean {
        return if (this.isShutDownThreadPools == null) java.lang.Boolean.TRUE else isShutDownThreadPools!!
    }

    fun withMinimumUploadPartSize(minimumUploadPartSize: Long?): MyTransferManagerBuilder {
        this.minimumUploadPartSize = minimumUploadPartSize
        return this
    }

    fun withMultipartUploadThreshold(multipartUploadThreshold: Long?): MyTransferManagerBuilder {
        this.multipartUploadThreshold = multipartUploadThreshold
        return this
    }

    fun withMultipartCopyThreshold(multipartCopyThreshold: Long?): MyTransferManagerBuilder {
        this.multipartCopyThreshold = multipartCopyThreshold
        return this
    }

    fun withMultipartCopyPartSize(multipartCopyPartSize: Long?): MyTransferManagerBuilder {
        this.multipartCopyPartSize = multipartCopyPartSize
        return this
    }

    fun withDisableParallelDownloads(disableParallelDownloads: Boolean?): MyTransferManagerBuilder {
        this.isDisableParallelDownloads = disableParallelDownloads
        return this
    }

    fun disableParallelDownloads(): MyTransferManagerBuilder {
        return this.withDisableParallelDownloads(java.lang.Boolean.TRUE)
    }

    private fun resolveConfiguration(): TransferManagerConfiguration {
        val configuration = TransferManagerConfiguration()
        if (this.minimumUploadPartSize != null) {
            configuration.minimumUploadPartSize = minimumUploadPartSize!!
        }

        if (this.multipartCopyPartSize != null) {
            configuration.multipartCopyPartSize = multipartCopyPartSize!!
        }

        if (this.multipartCopyThreshold != null) {
            configuration.multipartCopyThreshold = multipartCopyThreshold!!
        }

        if (this.multipartUploadThreshold != null) {
            configuration.multipartUploadThreshold = multipartUploadThreshold!!
        }

        if (this.isDisableParallelDownloads != null) {
            configuration.isDisableParallelDownloads = isDisableParallelDownloads!!
        }

        return configuration
    }

    private val params: TransferManagerParams
        get() = TransferManagerParams().withS3Client(this.resolveS3Client())
            .withExecutorService(this.resolveExecutorService()).withShutDownThreadPools(
            this.resolveShutDownThreadPools()
        ).withTransferManagerConfiguration(this.resolveConfiguration())

    fun build(): MyTransferManager? {
        return transferManagerFactory.apply(this.params) as MyTransferManager?
    }

    companion object {
        private val DEFAULT_TRANSFER_MANAGER_FACTORY: SdkFunction<TransferManagerParams?, TransferManager?> =
            SdkFunction { params ->
                MyTransferManager(
                    params!!
                )
            }

        @JvmStatic
        fun standard(): MyTransferManagerBuilder {
            return MyTransferManagerBuilder()
        }

        fun defaultTransferManager(): TransferManager? {
            return standard().build()
        }
    }
}
